package org.libra {
	import flash.events.Event;
	import flash.text.TextFieldType;
	import org.libra.basic.Container;
	import org.libra.events.ScrollEvent;
	import org.libra.geom.IntDimension;
	import org.libra.geom.IntPoint;
	import org.libra.ui.interfaces.IScrollPanelState;
	import org.libra.utils.JColor;
	
	/**
	 * ...
	 * @author zhangzuoqiang
	 */
	public class JTextArea extends Container {
		
		protected var textField:JTextField;
		protected var scrollPanel:JScrollPanel;
		protected var toBottom:Boolean;
		protected var scrollState:IScrollPanelState;
		
		public function JTextArea(text:String = "") {
			super();
			textField = new JTextField(text);
			textField.setTextFiledType(TextFieldType.DYNAMIC);
			textField.setMultiline(true);
			textField.setWordWrap(true);
			textField.setSelectable(false);
			this.append(textField);
			toBottom = true;
		}
		
		override public function setForeground(c:JColor):void {
			super.setForeground(c);
			this.textField.setForeground(c);
		}
		
		override public function setOpaque(b:Boolean):void {
			super.setOpaque(b);
			this.textField.setOpaque(b);
		}
		
		override protected function onAddToStageHandler(e:Event):void {
			super.onAddToStageHandler(e);
			if (this.scrollPanel) {
				this.scrollPanel.addEventListener(ScrollEvent.MOVE, onScrollMoveHandler);
			}
		}
		
		override protected function onRemoveFromStageHandler(e:Event):void {
			super.onRemoveFromStageHandler(e);
			if (this.scrollPanel) {
				if (this.scrollPanel.hasEventListener(ScrollEvent.MOVE)) {
					this.scrollPanel.removeEventListener(ScrollEvent.MOVE, onScrollMoveHandler);
				}
			}
		}
		
		protected function onScrollMoveHandler(e:ScrollEvent):void {
			this.textField.setScrollV(e.getCurLine() + 1);
		}
		
		override public function setSize(size:IntDimension):void {
			super.setSize(size);
			this.textField.setSize(new IntDimension(size.getWidth() - 20, size.getHeight()));
		}
		
		protected function showScrollBar():void {
			if (this.textField.getTextHeight() + this.textField.getLineHeight() >= getHeight()) {
				if (!scrollPanel) {
					scrollPanel = createScrollPanel();
				}
				if (this.append(scrollPanel)) {
					scrollPanel.addEventListener(ScrollEvent.MOVE, onScrollMoveHandler);
				}
				scrollPanel.setSize(new IntDimension(this.textField.getLineHeight(), getHeight()));
				scrollPanel.setLocation(new IntPoint(this.getWidth() - scrollPanel.getWidth()));
				scrollPanel.updateThumbHeight(textField.getMaxScrollV() - 1);
				scrollPanel.resetState();
				this.bringToTop(scrollPanel);
			}else {
				if (this.remove(scrollPanel)) {
					scrollPanel.removeEventListener(ScrollEvent.MOVE, onScrollMoveHandler);
				}
			}
		}
		
		protected function createScrollPanel():JScrollPanel {
			var s:JScrollPanel = new JScrollPanel(this.scrollState);
			s.setToBottom(this.toBottom);
			return s;
		}
		
		public function setToBottom(value:Boolean):void {
			this.toBottom = value;
			if (this.scrollPanel) {
				this.scrollPanel.setToBottom(value);
			}
		}
		
		public function setScrollState(state:IScrollPanelState):void {
			this.scrollState = state;
			if (state && this.scrollPanel)
				this.scrollPanel.setState(this.scrollState);
		}
		
		public function setText(text:String):void {
			this.textField.setText(text);
			showScrollBar();
		}
		
		public function appendText(text:String):void {
			this.textField.appendText(text);
			showScrollBar();
		}
		
		public function setHtmlText(text:String):void {
			this.textField.setHtmlText(text);
			showScrollBar();
		}
		
		public function getScrollPanel():JScrollPanel {
			return this.scrollPanel;
		}
		
		public function getTextField():JTextField {
			return this.textField;
		}
		
	}

}